// ------------------------用户操作-----------------------
function logout() {
    $.ajax("../api.php/logout", {
        type: "GET",
        dataType: "json",
        success: function (res, status_code) {
            console.log(res);
            if (res["code"] === 200) {
                localStorage.clear();
                location.replace('../login.html');
            }
        },
        error: function () {
            alert('失败');
        }
    });
}

/* 调用登录的api */
function login(userID, password) {
    // let userID = $("#userID").val();
    // let password = $("#password").val();
    if (userID === "" || password === "") {
        alert('用户名或者密码为空');
    } else {
        let password_crypto = md5(password);
        $.ajax("api.php/login", {
            type: "POST",
            dataType: "json",
            data: {'id': userID, 'password': password_crypto},
            success: function (res, status_code) {
                console.log('login', res);
                // alert(data);
                if (res["code"] == 200) {
                    console.log(res["data"]);
                    localStorage.setItem("userID", res["userID"]);
                    localStorage.setItem("user_data", JSON.stringify(res["data"]));
                    if (res["data"].user_type === "teacher") {
                        location.assign("./teacher/admin.html");
                    } else {
                        location.assign("./student/student.html");
                    }
                    // location.assign("./home.php");
                    // $.cookie('name', 'value', { expires: 7 });
                } else {
                    alert("用户名或者密码错误");
                }
            },
            error: function () {
                alert('出错了');
            }
        });
    }
}

function register(id, password, user_type) {
    // let patt_usname = /^[A-Za-z0-9]{5,20}$/
    // let patt_num = /^[0-9]{13}$/;
    let message = "";
    if (id === "" || password === "") {
        message = '用户名或者密码或者学号为空';
        alert(message);
    } else {
        console.log(id, password, user_type);
        let password_crypto = md5(password);
        $.ajax({
            url: "api.php/register",
            type: "POST",
            dataType: "json",
            data: {'id': id, 'password': password_crypto, 'user_type': user_type},
            success: function (res) {
                console.log('register', res);
                if (res["code"] === 200) {
                    if (user_type == 0)
                        location.assign("./student/student.html");
                    else if (user_type == 1) {
                        alert("注册成功但，请等待管理员审核");
                        location.assign("./login.html");
                    }
                } else {
                    alert("用户名已存在！");
                }
            },
            error: function () {
                alert('出错了');
            }
        });
    }
}

function change_password(new_password) {
    if (new_password === "") {
        alert("新密码为空");
    } else {
        let password_crypto = md5(new_password);
        $.ajax({
            url: "../api.php/change_password",
            type: "POST",
            dataType: "json",
            data: {'new_password': password_crypto},
            success: function (res) {
                if (res["code"] === 200) {
                    alert("修改成功");
                    location.reload();
                } else {
                    alert("修改密码失败，请重试");
                }
            },
            error: function () {
                alert('出错了');
            }
        });
    }
}